<template>
  <div>
    <!-- 导航栏 -->
    <div
      class="indexNavOut"
      :style="$route.name === 'center' ? { 'background-color': '#ffffff' } : ''"
    >
      <div class="indexNav">
        <div class="navLi">
          <ul class="navlist">
            <li>
              <el-link href="/" :underline="false">优优商城</el-link>
            </li>
            <li>
              <div style="color: #606266; font-size: 14px">
                <i class="el-icon-location-outline"></i>&nbsp;{{
                  $store.state.address ? $store.state.address.district : ""
                }}
              </div>
            </li>
          </ul>
          <ul class="navright">
            <li>
              <el-link href="/myorder" :underline="false"> 我的订单</el-link>
            </li>
            <li>
              <el-link href="/shoppingcart" :underline="false">购物车</el-link>
            </li>
            <li>
              <div v-if="!$store.state.isLogin">
                <el-link href="/login" :underline="false" @click="handelLogin"
                  >您好，请登录</el-link
                >
              </div>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <!-- 搜索 -->
    <div
      v-if="$route.name !== 'center' && $route.name !== 'order'"
      class="indexSearch"
    >
      <div class="logo">
        <el-link href="/" :underline="false">
          <img src="../../assets/logo.png" width="150" height="80" />
        </el-link>
      </div>
      <div class="search">
        <el-input
          placeholder="搜索"
          v-model="searchValue"
          @keyup.enter.native="search"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="search"
          >搜索</el-button
        >
      </div>

      <div class="user" v-if="$store.state.isLogin">
        <div class="av">
          <el-link :underline="false" href="/center">
            <Avatar
              :avatar="{
                id: 0,
                username: $store.state.user.nickname,
                txcolor: $store.state.user.txcolor,
                bgcolor: $store.state.user.bgcolor,
              }"
            />
          </el-link>
        </div>
        <div class="info">
          <div><span>Hi,</span>{{ $store.state.user.nickname }}</div>
          <div class="cz">
            <el-link :underline="false" href="/center" icon="el-icon-s-custom"
              >我的</el-link
            >
            <el-link @click="logout"  :underline="false" icon="el-icon-switch-button"
              >退出</el-link
            >
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Avatar from '../../components/Avatar'
import getCurrentCityName from '../../utils/BMap'
import { isAuth, getLoginInfo, removeToken, removeLoginInfo } from '../../utils/auth'

export default {
  components: {
    Avatar
  },
  data () {
    return {
      searchValue: ''
      // v-if="!isLogin"
    }
  },
  mounted () {
    this.$nextTick(() => {
      getCurrentCityName().then((address) => {
        this.$store.commit('setLocation', address)
      })
    })
    if (isAuth()) {
      this.$store.commit('handleLogin', true)
      this.$store.commit('setUserInfo', getLoginInfo())
    } else {
      this.$store.commit('handleLogin', false)
    }
  },
  methods: {
    logout () {
      this.$store.commit('handleLogin', false)
      // 清除token
      removeToken()
      // 清除用户信息
      removeLoginInfo()
    },
    handelLogin () {
      // if (this.$store.state.isLogin === true) {
      //   this.islogin = false
      // } else {
      //   this.islogin = true
      // }
    },
    search () {
      this.$router.push({ name: 'search', query: { q: this.searchValue } })
    },
    handleAdd () {}
  }
}
</script>
<style lang="scss">
.indexSearch {
  .search {
    .el-input {
      .el-input__inner {
        border-radius: 0px;
      }
    }
    .el-button {
      border-radius: 0px;
      height: 40px;
    }
  }
}
</style>
<style lang="scss" scoped>
.indexSearch {
  display: flex;
  align-items: center;
  margin-top: 10px;
  height: 100px;
  // margin: 0 auto;
  .logo {
    margin-left: 160px;
  }
  .search {
    display: flex;
    // widows: 330px;
    height: 41px;
    margin-left: calc(50vw - 150px - 450px);
    width: 680px;
    // .button{
    //   background-color: #e4393c;
    // }
  }
  .el-input {
    // margin-right: 20px;
    .el-input__inner {
      border-radius: 0px;
    }
    border-radius: 0px;
    width: 500px;
  }
  .user {
    width: 170px;
    height: 100px;
    margin-left: 50px;
    border-bottom: 1px solid rgb(235, 209, 207);
    border-right: 1px solid rgb(235, 209, 207);

    display: flex;
    min-width: 170px;
    .av,
    .info {
      margin-top: 20px;
    }
    .info {
      margin-left: 10px;
      // border: 1px solid;
      padding-top: 10px;
      .el-link:hover {
        color: #e2231a;
      }
      .cz {
        margin-top: 10px;
        .el-link:nth-child(1) {
          margin-right: 5px;
        }
      }
    }
  }
}
</style>
<style lang="scss" scoped>
.indexNavOut {
  display: flex;
  height: 40px;
  background-color: #f2f2f2;

  .el-link:hover {
    color: #e2231a;
  }
}
.indexNav {
  width: 100%;
  margin: 0 auto;
}
.navLi {
  display: flex;
  justify-content: space-between;
  .navlist {
    width: 350px;
    display: flex;
    list-style: none;
    margin-left: 80px;
    // margin-right: 250px;
  }
  .navright {
    width: 350px;
    display: flex;
    list-style: none;
    // float: left;
    // margin-left: 250px;
  }
}
.navLi {
  .navlist li {
    flex: 1;
    line-height: 40px;
    text-align: center;
    // margin-right: 20px;
    position: relative;
  }
  .navright li {
    flex: 1;
    height: 40px;
    line-height: 40px;
    position: relative;
  }
}
</style>
